package com.example.excel.data;

import java.io.Serializable;

/**
 * @author inggg
 * @date 2018/5/11 15:28
 */
@SuppressWarnings("unused")
public class CommonResponse<T> implements Serializable {
    private static final long serialVersionUID = 5614088349949206033L;

    private String returnCode;
    private String returnMsg;
    private T data;

    public static <T> CommonResponse<T> success(String msg) {
        CommonResponse<T> resp = new CommonResponse<>();
        resp.setReturnCode("SUCCESS");
        resp.setReturnMsg(msg);
        return resp;
    }

    public static <T> CommonResponse<T> success(T data, String msg) {
        CommonResponse<T> resp = new CommonResponse<>();
        resp.setReturnCode("SUCCESS");
        resp.setData(data);
        resp.setReturnMsg(msg);
        return resp;
    }

    public static <T> CommonResponse<T> response(T data, String msg, String code) {
        CommonResponse<T> resp = new CommonResponse<>();
        resp.setReturnCode(code);
        resp.setData(data);
        resp.setReturnMsg(msg);
        return resp;
    }

    public static <T> CommonResponse<T> error(String msg) {
        CommonResponse<T> resp = new CommonResponse<>();
        resp.setReturnMsg(msg);
        resp.setReturnCode("FAIL");
        return resp;
    }

    public static <T> CommonResponse<T> unauthorized(String msg) {
        CommonResponse<T> resp = new CommonResponse<>();
        resp.setReturnMsg(msg);
        resp.setReturnCode("UNAUTHORIZED");
        return resp;
    }

    public String getReturnCode() {
        return returnCode;
    }

    public void setReturnCode(String returnCode) {
        this.returnCode = returnCode;
    }

    public String getReturnMsg() {
        return returnMsg;
    }

    public void setReturnMsg(String returnMsg) {
        this.returnMsg = returnMsg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "{" +
                "returnCode:" + returnCode +
                ", returnMsg:" + returnMsg +
                ", data:" + data +
                '}';
    }

    public static boolean isSuccess(CommonResponse<?> commonResponse) {
        return commonResponse != null
                && "SUCCESS".equalsIgnoreCase(commonResponse.getReturnCode());
    }

    public static boolean isFail(CommonResponse<?> commonResponse) {
        return !isSuccess(commonResponse);
    }
}
